# koroFileHeader at Home PC
# Create: 2022-11-13 14:23:20
# LastEdit: 2022-11-13 16:32:43
"""说明暂无"""
__author__ = '749B'

from django.contrib import admin
from django.db import models

from .report import PriceSheet
from .validators import validate_range100


class CppProduct(models.Model):
    product = models.CharField(max_length=256, verbose_name='产品', unique=True)
    meter_sub_category = models.CharField(max_length=128, verbose_name='服务类型')
    meter_name = models.CharField(max_length=128, verbose_name='服务资源')
    meter_region = models.CharField(max_length=128, verbose_name='服务区域')
    cpp_count = models.SmallIntegerField(validators=validate_range100, default=0, verbose_name='CPP数量')
    VALUE_OF_MEASURE_CHOICES = [(1, '1 Hour'), (10, '10 Hours'), (100, '100 Hours'), (1000, '1000 Hours')]
    value_of_measure = models.IntegerField(choices=VALUE_OF_MEASURE_CHOICES, default=1, verbose_name='度量值')
    unit_price = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True, verbose_name='单位价格')  # 例如 ¥1,919.81
    locked = models.BooleanField(default=False, verbose_name='禁止自动修改')
    
    price = models.ForeignKey(PriceSheet, on_delete=models.SET_NULL, blank=True, null=True)

    def __str__(self):
        return self.product.removeprefix('Virtual Machines').strip()

    class Meta:
        verbose_name = 'CPP录入'
        verbose_name_plural = 'CPP数量录入表'

    @admin.display(description='价格')
    def price_display(self):
        if self.unit_price:
            return self.unit_price / self.value_of_measure
        if self.price:
            return self.price.unit_price / self.price.value_of_measure

